package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.page.PageParams;
import com.sky.annotate.AuotFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ThreadUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println("md5:"+password);

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /*  分页查询  */
    @Override
    public PageResult page(EmployeePageQueryDTO page) {
        if (page.getPage() ==null){
            page.setPage(1);
        }
        if (page.getPageSize() ==null){
            page.setPageSize(10);
        }

        PageHelper.startPage(page.getPage(),page.getPageSize());
        Page<Employee> page1 = employeeMapper.page(page);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page1.getTotal());
        pageResult.setRecords(page1.getResult());
        return pageResult;
    }

    /* 新增员工  */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addEmp(EmployeeDTO employeeDTO) throws Exception {
        //存储对象不为空
        if (employeeDTO==null){
            throw new Exception(MessageConstant.OBJECT_NULL);
        }

        //验证账号是否存在
        Employee byUsername = employeeMapper.getByUsername(employeeDTO.getUsername());
        if (byUsername!=null){
            throw new Exception(MessageConstant.ALREADY_EXISTS);
        }
        //获取对应的数据库封装类，并初始化数据
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.addEmp(employee);
    }

    /*修改员工*/
    @Override

    public void UpDate(Employee employee) {
        log.error(employee.toString());
        employeeMapper.UpDate(employee);
    }

    @Override
    public Employee getEmp(Integer id) throws Exception {
        Integer status = employeeMapper.getStatusById(id);
        Employee empById;
        //用户账号是否锁定
        if (status ==1){
            throw new Exception(MessageConstant.ACCOUNT_NO_LOCKED);
        }else if (status ==0){
             empById = employeeMapper.getEmpById(id);
            return empById;
        }else {
            //是否存在用户不存在直接抛出异常
            throw new Exception(MessageConstant.ACCOUNT_NOT_FOUND);
        }

    }

}
